package com.test.example.order;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.ljdp.api.client.ApiClient;
import org.ljdp.api.client.ApiRequestEntity;
import org.ljdp.component.result.BasicApiResponse;
import org.ljdp.component.result.DataApiResponse;
import org.ljdp.ui.bootstrap.TablePage;
import org.ljdp.ui.bootstrap.TablePageImpl;

import com.openjava.example.order.domain.ExampleOrder;
import com.openjava.example.order.vo.ExampleOrderRequest;

@DisplayName("订单例子测试")
public class ExampleOrderTestCase {
	private static String tokenid;
	private static String userAgent;
	
	private static List<Long> saveIds = new ArrayList<>();
	
	@BeforeAll
	static void init() {
		tokenid = "YxmtaXb4dTtJ-9rXu57jnaUvh9volzLK5HpwzL3_9Mk";
		userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
	}
	
	@DisplayName("新增数据(表单提交)")
	@Test
	void test1001() {
		System.out.println("============add1=============");
		try {
			ApiRequestEntity apientity = new ApiRequestEntity(null);
			String apiUrl = "/api/example/order/exampleOrder";
			apientity.setUrl(apiUrl);
			apientity.addParam("tokenid", tokenid);
			apientity.addParam("userAgent", userAgent);
			apientity.addParam("operAccount", "单元测试");
			apientity.addParam("userName", "单元测试");
			apientity.addParam("userAddress", "单元测试");
			apientity.addParam("totalPrice", "1998");
			apientity.addParam("submitTime", "2018-08-08 08:08:08");
			apientity.addParam("isNew", "true");
			
			DataApiResponse resp = ApiClient.doPost(apientity, DataApiResponse.class);
			
			assertEquals(resp.getCode().intValue(), 200, resp.getMessage());
			assertNull(resp.getMessage());
			
			String result = ToStringBuilder.reflectionToString(resp, ToStringStyle.MULTI_LINE_STYLE);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
	
	@DisplayName("新增数据(json)")
	@Test
	void test1002() {
		System.out.println("============add2=============");
		try {
			ApiRequestEntity apientity = new ApiRequestEntity(null);
			String apiUrl = "/api/example/order/exampleOrder/save";
			apientity.setUrl(apiUrl);
			apientity.setToken(tokenid);
			apientity.setUserAgent(userAgent);
			//提交对象
			ExampleOrderRequest reqBody = new ExampleOrderRequest();
			reqBody.setUserName("单元测试");
			reqBody.setUserAddress("单元测试");
			reqBody.setTotalPrice(123L);
			reqBody.setOperAccount("单元测试");
			reqBody.setSubmitTime(new Date());
			reqBody.setIsNew(true);
			apientity.setBody(reqBody);
			
			DataApiResponse resp = ApiClient.doPostJson(apientity, DataApiResponse.class);
			
			assertEquals(resp.getCode().intValue(), 200, resp.getMessage());
			assertNull(resp.getMessage());
			
			String result = ToStringBuilder.reflectionToString(resp, ToStringStyle.MULTI_LINE_STYLE);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	@DisplayName("搜索（动态参数）")
	@Test
	void test1005() {
		System.out.println("============search=============");
		try {
			ApiRequestEntity apientity = new ApiRequestEntity(null);
			String apiUrl = "/api/example/order/exampleOrder/search";
			apientity.setUrl(apiUrl);
			apientity.addParam("tokenid", tokenid);
			apientity.addParam("userAgent", URLEncoder.encode(userAgent,"utf-8"));//GET请求需要编码
			//[BEGIN]下面添加查询条件
			apientity.addParam("eq_operAccount", "单元测试");
			apientity.addParam("page", "0");
			apientity.addParam("size", "20");
			//[END]
			@SuppressWarnings("unchecked")
			TablePage<ExampleOrder> resp = ApiClient.doGet(apientity,
					TablePageImpl.class, TablePage.class, ExampleOrder.class);
			
			assertEquals(resp.getCode().intValue(), 200, resp.getMessage());
			if(resp.getRows().isEmpty()) {
				fail("无数据");
			}
			
			//把id保存起来，下面测试删除
			resp.getRows().forEach(f -> {
				saveIds.add(f.getOrderId());
			});
			
			String result = ToStringBuilder.reflectionToString(resp, ToStringStyle.MULTI_LINE_STYLE);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
	
	@DisplayName("删除")
	@Test
	void test1009() {
		System.out.println("============remove=============");
		try {
			ApiRequestEntity apientity = new ApiRequestEntity(null);
			String apiUrl = "/api/example/order/exampleOrder/remove";
			apientity.setUrl(apiUrl);
			apientity.addParam("tokenid", tokenid);
			apientity.addParam("userAgent", userAgent);
			//添加删除的id
			StringBuffer ids = new StringBuffer();
			saveIds.forEach(id -> {
				ids.append(id).append(",");
			});
			apientity.addParam("ids", ids.toString());
			
			BasicApiResponse resp = ApiClient.doPost(apientity, BasicApiResponse.class);
			
			assertEquals(resp.getCode().intValue(), 200, resp.getMessage());
			assertNull(resp.getMessage());
			
			String result = ToStringBuilder.reflectionToString(resp, ToStringStyle.MULTI_LINE_STYLE);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
}
